<!-- This comment will put IE 6, 7 and 8 in quirks mode -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>Código-Fonte de semaphore.h</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javaScript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body onload='searchBox.OnSelectItem(0);'>
<!-- Gerado por Doxygen 1.6.3 -->
<script type="text/javascript"><!--
var searchBox = new SearchBox("searchBox", "search",false,'Busca');
--></script>
<div class="navigation" id="top">
  <div class="tabs">
    <ul>
      <li><a href="index.html"><span>Página&nbsp;Principal</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Arquivos</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <img id="MSearchSelect" src="search/search.png"
             onmouseover="return searchBox.OnSearchSelectShow()"
             onmouseout="return searchBox.OnSearchSelectHide()"
             alt=""/>
        <input type="text" id="MSearchField" value="Busca" accesskey="S"
             onfocus="searchBox.OnSearchFieldFocus(true)" 
             onblur="searchBox.OnSearchFieldFocus(false)" 
             onkeyup="searchBox.OnSearchFieldChange(event)"/>
        <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
        </div>
      </li>
    </ul>
  </div>
  <div class="tabs">
    <ul>
      <li><a href="files.html"><span>Lista&nbsp;de&nbsp;Arquivos</span></a></li>
      <li><a href="globals.html"><span>Arquivos&nbsp;Membros</span></a></li>
    </ul>
  </div>
<h1>semaphore.h</h1><a href="semaphore_8h.html">Vá para a documentação deste arquivo.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 
<a name="l00008"></a>00008 <span class="preprocessor">#ifndef SEMAPHORE_H</span>
<a name="l00009"></a>00009 <span class="preprocessor"></span><span class="preprocessor">#define SEMAPHORE_H</span>
<a name="l00010"></a>00010 <span class="preprocessor"></span>
<a name="l00011"></a>00011 <span class="preprocessor">#include &lt;boost/thread/mutex.hpp&gt;</span>
<a name="l00012"></a>00012 <span class="preprocessor">#include &lt;boost/thread/condition.hpp&gt;</span>
<a name="l00013"></a>00013 <span class="preprocessor">#include &lt;iostream&gt;</span>
<a name="l00014"></a>00014 
<a name="l00015"></a>00015 <span class="comment">/* ----------------------------------------*/</span>
<a name="l00019"></a>00019 <span class="comment">/* ----------------------------------------*/</span>
<a name="l00020"></a><a class="code" href="classsemaphore.html">00020</a> <span class="keyword">class </span><a class="code" href="classsemaphore.html">semaphore</a>
<a name="l00021"></a>00021 {
<a name="l00022"></a>00022   <span class="keyword">public</span>:
<a name="l00023"></a>00023 
<a name="l00024"></a>00024     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00028"></a>00028     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00029"></a><a class="code" href="classsemaphore.html#ad4d360a8eabfead011d7ce5394c4dd2a">00029</a>     <span class="keyword">typedef</span> boost::mutex::scoped_lock <a class="code" href="classsemaphore.html#ad4d360a8eabfead011d7ce5394c4dd2a">scoped_lock</a>;
<a name="l00030"></a>00030 
<a name="l00031"></a>00031     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00038"></a>00038     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00039"></a><a class="code" href="classsemaphore.html#a77d85322407545b8ccc4a85b920d90c1">00039</a>     <a class="code" href="classsemaphore.html#a77d85322407545b8ccc4a85b920d90c1">semaphore</a> (<span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span>&amp; initCount,<span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span>&amp; maxCount)
<a name="l00040"></a>00040       :_myCount(initCount),_myMaxCount(maxCount){}
<a name="l00041"></a>00041 
<a name="l00042"></a>00042     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00046"></a>00046     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00047"></a><a class="code" href="classsemaphore.html#af0e430d192c44be1d65f5a4fd8cb91a4">00047</a>    <span class="keywordtype">void</span> <a class="code" href="classsemaphore.html#af0e430d192c44be1d65f5a4fd8cb91a4">wait</a> (<span class="keywordtype">void</span>)
<a name="l00048"></a>00048    {
<a name="l00049"></a>00049      <span class="keywordflow">try</span>
<a name="l00050"></a>00050      {
<a name="l00051"></a>00051        <a class="code" href="classsemaphore.html#ad4d360a8eabfead011d7ce5394c4dd2a">scoped_lock</a> lock(_myMutex);
<a name="l00052"></a>00052        <span class="keywordflow">if</span> (_myCount &gt; 0 )
<a name="l00053"></a>00053        {
<a name="l00054"></a>00054          _myCount--;
<a name="l00055"></a>00055          <span class="keywordflow">if</span> (_myCount==_myMaxCount-1)
<a name="l00056"></a>00056            _myCondition.notify_all();
<a name="l00057"></a>00057        }
<a name="l00058"></a>00058        <span class="keywordflow">else</span>
<a name="l00059"></a>00059        {
<a name="l00060"></a>00060          <a class="code" href="classsemaphore.html#ad4d360a8eabfead011d7ce5394c4dd2a">scoped_lock</a>(_myMutexCondition);
<a name="l00061"></a>00061          <span class="keywordflow">while</span>(_myCount &lt;= 0 )
<a name="l00062"></a>00062            _myCondition.wait(_myMutex);
<a name="l00063"></a>00063          _myCount--;
<a name="l00064"></a>00064        }
<a name="l00065"></a>00065      }
<a name="l00066"></a>00066      <span class="keywordflow">catch</span> (std::exception&amp; ex)
<a name="l00067"></a>00067      {
<a name="l00068"></a>00068        std::cout &lt;&lt; <span class="stringliteral">&quot;Erro: &quot;</span> &lt;&lt; ex.what() &lt;&lt; std::endl;
<a name="l00069"></a>00069        exit(1);
<a name="l00070"></a>00070      }
<a name="l00071"></a>00071    }
<a name="l00072"></a>00072 
<a name="l00073"></a>00073    <span class="comment">/* ----------------------------------------*/</span>
<a name="l00077"></a>00077    <span class="comment">/* ----------------------------------------*/</span>
<a name="l00078"></a><a class="code" href="classsemaphore.html#a14c3b9f4312c43d9400905c938752665">00078</a>    <span class="keywordtype">void</span> <a class="code" href="classsemaphore.html#a14c3b9f4312c43d9400905c938752665">post</a> (<span class="keywordtype">void</span>)
<a name="l00079"></a>00079    {
<a name="l00080"></a>00080      <span class="keywordflow">try</span>
<a name="l00081"></a>00081      {
<a name="l00082"></a>00082        <a class="code" href="classsemaphore.html#ad4d360a8eabfead011d7ce5394c4dd2a">scoped_lock</a> lock(_myMutex);
<a name="l00083"></a>00083        <span class="keywordflow">if</span> (_myCount+1 &lt;= _myMaxCount)
<a name="l00084"></a>00084        {
<a name="l00085"></a>00085          _myCount++;
<a name="l00086"></a>00086          <span class="keywordflow">if</span> (_myCount==1)
<a name="l00087"></a>00087            _myCondition.notify_all();
<a name="l00088"></a>00088        }
<a name="l00089"></a>00089        <span class="keywordflow">else</span>
<a name="l00090"></a>00090        {
<a name="l00091"></a>00091          <a class="code" href="classsemaphore.html#ad4d360a8eabfead011d7ce5394c4dd2a">scoped_lock</a> (_myMutexCondition);
<a name="l00092"></a>00092          <span class="keywordflow">while</span>(_myCount+1 &gt;= _myMaxCount)
<a name="l00093"></a>00093            _myCondition.wait(_myMutex);
<a name="l00094"></a>00094          _myCount++;
<a name="l00095"></a>00095        }
<a name="l00096"></a>00096      }
<a name="l00097"></a>00097      <span class="keywordflow">catch</span> (std::exception&amp; ex)
<a name="l00098"></a>00098      {
<a name="l00099"></a>00099        std::cout &lt;&lt; <span class="stringliteral">&quot;Erro: &quot;</span> &lt;&lt; ex.what() &lt;&lt; std::endl;
<a name="l00100"></a>00100        exit(1);
<a name="l00101"></a>00101      }
<a name="l00102"></a>00102    }
<a name="l00103"></a>00103 
<a name="l00104"></a>00104   <span class="keyword">private</span>:
<a name="l00105"></a>00105 
<a name="l00106"></a>00106     boost::mutex _myMutex;
<a name="l00107"></a>00107     boost::condition _myCondition;
<a name="l00108"></a>00108     <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> _myCount;
<a name="l00109"></a>00109     <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> _myMaxCount;
<a name="l00110"></a>00110 };
<a name="l00111"></a>00111 
<a name="l00112"></a>00112 <span class="preprocessor">#endif //SEMAPHORE_H</span>
</pre></div></div>
<!--- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&nbsp;</span>Todos</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&nbsp;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&nbsp;</span>Arquivos</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&nbsp;</span>Funções</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&nbsp;</span>Variáveis</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&nbsp;</span>Definições de Tipos</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&nbsp;</span>Amigas</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<hr class="footer"/><address style="text-align: right;"><small>Gerado em Sat Oct 30 13:47:50 2010 por&nbsp;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.3 </small></address>
</body>
</html>
